package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.CorrectExamDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.CorrectExamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * @Author 罗蔚
 * @Date 2022/5/7 14:23
 * @Description TODO
 */
@Service
public class CorrectExamServiceImpl implements CorrectExamService {
    @Resource
    CorrectExamDao correctExamDao;

    @Override
    public ResponseEntity showAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        //查所有的发布的考试
        List<ExamPublish> examPublishList = correctExamDao.showAll();
        PageInfo<ExamPublish> pageInfo = PageInfo.of(examPublishList);
        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity findLike(ExamPublish examPublish, int currentPage, int pageSize) {
//        System.out.println(examPublish);
        PageHelper.startPage(currentPage, pageSize);
        //查所有的发布的考试
        List<ExamPublish> examPublishList = correctExamDao.findLike(examPublish);

//        //处理是否审批
//        if (examPublish.getIsCorrect() > 0) {
//            Iterator<ExamPublish> iterator = examPublishList.iterator();
//            while (iterator.hasNext()) {
//                ExamPublish examPublish1 = iterator.next();
//                if (examPublish1.getIsCorrect() == 0) {
//                    iterator.remove();
//                }
//            }
//        }

        PageInfo<ExamPublish> pageInfo = PageInfo.of(examPublishList);
        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity findExamineeIsCorrect(ExamPublish examPublish) {
        int eid = examPublish.getId();
        for (ExamineeInformation examineeInformation : examPublish.getExamineeInformationList()) {
            int sid = examineeInformation.getId();
            //循环查询，该套考试各个学生是否被批改
            int examineeIsCorrect = correctExamDao.findExamineeIsCorrect(eid, sid);
            examineeInformation.setExamineeIsCorrect(examineeIsCorrect);
        }

        //将未批改的放在前面
        Collections.sort(examPublish.getExamineeInformationList(), new Comparator<ExamineeInformation>() {
            @Override
            public int compare(ExamineeInformation o1, ExamineeInformation o2) {
                return o2.getExamineeIsCorrect() - o1.getExamineeIsCorrect();
            }
        });

        return new ResponseEntity(200, "查询成功", examPublish);
    }

    @Override
    public ResponseEntity findScore(int eid, int sid) {
        int i = correctExamDao.findStatus(eid, sid);
        System.out.println(i);
        //如果i == 0
        if (i == 0) {
            return new ResponseEntity(200, "还没有答卷", 0);
        } else {
            int score = correctExamDao.findScore(eid, sid);
            List<PaperObject> paperObjectList = correctExamDao.findPaperAll(eid);
            for (PaperObject paperObject : paperObjectList) {
                if (paperObject.getQuTypeId() == 2) {
                    score += paperObject.getScore();
                }
            }
            return new ResponseEntity(200, "已批改", score);
        }
    }

    @Override
    public ResponseEntity findQuestionAndCorrect(int eid, int sid) {
        //需要查出考生答案
        List<ExamOver> examOverList = correctExamDao.findAll(eid, sid);
//        System.out.println(examOverList);
        //查出试卷本来答案
        List<PaperObject> paperObjectList = correctExamDao.findPaperAll(eid);
//        System.out.println(paperObjectList);

        for (int i = 0; i < paperObjectList.size(); i++) {
            //将学生答案带到paperObjectList中
            for (ExamOver examOver : examOverList) {
                if (paperObjectList.get(i).getQuestionId() == examOver.getQuestionId()) {
                    paperObjectList.get(i).setStudentsAnswer(examOver.getStudentAnswer());
                    break;
                }
            }

            //是单选题或判断题，比较答案
            if (paperObjectList.get(i).getQuTypeId() == 1) {
                for (ExamOver examOver : examOverList) {
                    if (paperObjectList.get(i).getQuestionId() == examOver.getQuestionId()) {
                        //状态变为已批改
                        examOver.setCheckStatus(1);
                        //正确加分
                        if (paperObjectList.get(i).getAnswer().equals(examOver.getStudentAnswer())) {
                            examOver.setGetScore(paperObjectList.get(i).getScore());
                        }
                        break;
                    }
                }
            }
            if (paperObjectList.get(i).getQuTypeId() == 2) {
                //youbug
                for (ExamOver examOver : examOverList) {
                    if (paperObjectList.get(i).getQuestionId() == examOver.getQuestionId()) {
                        //状态变为已批改
                        examOver.setCheckStatus(1);
                        break;
                    }
                }
            }
            if (paperObjectList.get(i).getQuTypeId() == 3) {
                for (ExamOver examOver : examOverList) {
                    if (paperObjectList.get(i).getQuestionId() == examOver.getQuestionId()) {
                        //状态变为已批改
                        examOver.setCheckStatus(1);
                        //正确加分
                        if (paperObjectList.get(i).getAnswer().equals("正确") && examOver.getStudentAnswer().equals("1")) {
                            examOver.setGetScore(paperObjectList.get(i).getScore());
                        }
                        if (paperObjectList.get(i).getAnswer().equals("错误") && examOver.getStudentAnswer().equals("2")) {
                            examOver.setGetScore(paperObjectList.get(i).getScore());
                        }
                        break;
                    }
                }
            }
            //填空，问答vc
        }
//        examOverList.forEach(System.out::println);
//        paperObjectList.forEach(System.out::println);
        //将选择题和判断题的得分插入
        int i = correctExamDao.updateSome(examOverList);

        return new ResponseEntity(200, "查询成功", paperObjectList);
    }

    @Override
    public ResponseEntity commitCorrect(List<PaperObject> paperObjectList, int eid, int sid) {
        Iterator<PaperObject> iterator = paperObjectList.iterator();
        while (iterator.hasNext()) {
            PaperObject paperObject = iterator.next();
            if(paperObject.getQuTypeId() != 4 && paperObject.getQuTypeId() != 5){
                iterator.remove();
            }
        }
        int i = correctExamDao.updateOther(paperObjectList, eid, sid);
        if (i != 0) {
            return new ResponseEntity(200, "批改成功", i);
        } else {
            return new ResponseEntity(500, "批改失败", i);
        }
    }
}
